package com.asion.vmovie.ui.MVPModel;

import com.asion.vmovie.api.ApiService;
import com.asion.vmovie.beans.DetialsBean;
import com.asion.vmovie.beans.LunBoBean;
import com.asion.vmovie.ui.MVPContract.NewsContract;
import com.asion.vmovie.utils.retrofitCache.SetCacheUtils;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by asion on 16-10-18.
 */

public class NewsModel implements NewsContract.Model {
    @Override
    public void getNewsData(int p,int size,NewsContract.OnGetNewsSuccess onGetNewsSuccess) {
        Observable<DetialsBean> latest = new Retrofit.Builder()
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(SetCacheUtils.setCache())
                .baseUrl(ApiService.BASE_URL)
                .build()
                .create(ApiService.class)
                .getNewsCommend(p, size, "latest");
        latest.subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(detialsBean -> onGetNewsSuccess.onGetSuccess(detialsBean)
                ,throwable -> onGetNewsSuccess.onGetError(throwable));
    }

    @Override
    public void getBanner(NewsContract.OnGetBanner onGetBanner) {
        Observable<LunBoBean> banner =
                new Retrofit.Builder().addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(SetCacheUtils.setCache())
                .baseUrl(ApiService.BASE_URL)
                .build()
                .create(ApiService.class)
                .getBanner();
        banner.subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(lunBoBean -> onGetBanner.onSuccess(lunBoBean)
                ,throwable -> onGetBanner.onError(throwable));
    }

}
